package com.zhuiYun.project.api.newDaily.mapper;

import com.zhuiYun.project.api.commoditydetail.entity.Commodity;
import com.zhuiYun.project.api.newDaily.entity.UserIntegral;
import com.zhuiYun.project.api.newDaily.entity.dto.SlideshowDTO;
import com.zhuiYun.project.api.newDaily.entity.dto.UserCouponDTO;
import com.zhuiYun.project.api.newDaily.model.UserCouponModel;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Description TODO 每日上新（小程序）
 * @ClassName NewDailyMapper
 * @Author HH
 * @Date 2023/4/5 13:49
 **/
@Mapper
public interface NewDailyMapper {

    /**
    * @Description 查询主页轮播图
    * @Author HH
    * @Date 2023/4/5 14:40
    * @Return java.util.List<com.zhuiYun.project.api.newDaily.entity.SlideShow>
    
    */
    @Select("<script>SELECT s.image_id,s.carousel_id,c.url FROM slideshow s,carousel c WHERE s.commdiy_id = c.id AND s.pc_or_app = '1' AND s.diycomm_type = '3' AND s.delete_flag = '0' AND s.commdiy_id in(3,4) ORDER BY s.create_time DESC LIMIT 6</script>")
    List<SlideshowDTO> selectSlideShow();

    /**
    * @Description 查询今日上新
    * @Author HH
    * @Date 2023/4/5 14:15
    * @Return java.util.List<com.zhuiYun.project.api.commoditydetail.entity.Commodity>

    */
    @Select("<script>SELECT id,commodity_name,price,image_id  FROM commodity WHERE curdate() &lt; create_time AND del_flag = '0'</script>")
    List<Commodity> selectCommodityNew();

    /**
    * @Description 查询拼团好货
    * @Author HH
    * @Date 2023/4/6 10:46
    * @Return java.util.List<com.zhuiYun.project.api.commoditydetail.entity.Commodity>
    
    */
    @Select("<script>SELECT id,commodity_name,price,favorable_price,image_id  FROM commodity WHERE del_flag = '0' order by rand()</script>")
    List<Commodity> selectCommodity();

    /**
    * @Description 根据用户id查询用户可用的优惠券
    * @Author HH
    * @Date 2023/4/6 21:19
    * @Return java.util.List<com.zhuiYun.project.api.newDaily.entity.Coupon>
    * @param userId
    */
    @Select("<script>SELECT uc.coupon_id,c.id,c.full,c.discounts,c.threshold,c.create_time,c.expire_time FROM coupon c LEFT JOIN user_coupon uc on c.id = uc.coupon_id AND uc.user_id = #{userId} AND curdate() &lt; uc.expire_time WHERE c.delete_flag = '0' AND curdate() &lt; c.expire_time</script>")
    @Results({
            @Result(property = "couponId",column = "coupon_id"),
    })
    List<UserCouponDTO> selectUserCoupon(Integer userId);

    /**
     * @Description 添加用户优惠券
     * @Author HH
     * @Date 2023/4/7 9:53
     * @Return int
     * @param userCouponModel
     */
    @Insert("<script>INSERT INTO user_coupon (user_id,coupon_id,create_time,expire_time) VALUES(#{userId},#{couponId},now(),#{expireTime})</script>")
    int insertUserCoupon(UserCouponModel userCouponModel);

    /**
     * @Description 查询用户积分
     * @Author HH
     * @Date 2023/4/8 11:44
     * @Return com.zhuiYun.project.api.newDaily.entity.UserIntegral

     */
    @Select("<script>SELECT integral FROM user_integral WHERE user_id = #{userId}</script>")
    UserIntegral selectUserIntegral(Integer userId);

    /**
    * @Description 新增数据：签到成功增加用户积分
    * @Author HH
    * @Date 2023/4/13 19:05
    * @Return com.zhuiYun.project.api.newDaily.entity.UserIntegral
    * @param userIntegral
    */
    @Insert("<script>INSERT INTO user_integral (user_id,integral,create_time) VALUES(#{userId},#{integral},now())</script>")
    int insertUserIntegral(UserIntegral userIntegral);

    /**
    * @Description 修改数据: 签到成功增加用户积分
    * @Author HH
    * @Date 2023/4/13 20:46
    * @Return int
    * @param userIntegral
    */
    @Update("<script>UPDATE user_integral SET integral = #{integral} WHERE user_id = #{userId}</script>")
    int updateUserIntegral(UserIntegral userIntegral);
}
